Ext.define('Emp.controller.EmployeeController', {
    extend: 'Emp.controller.BaseController',

    models: ['Employee', 'Dept'],
    views: ['employee.List', 'employee.Edit'],
    refs: [
        {ref: 'employeeList', selector: 'employeelist'},
        {ref: 'employeeEdit', selector: 'employeeedit', xtype: 'employeeedit', autoCreate: true}
    ],
    
    init: function() {
        this.control({
        	'employeelist treepanel': {
        		'itemclick': this.doClickOnTree
        	},
        	'employeelist grid': {
        		'itemdblclick': this.doModifyOnList
        	},
            'employeelist gridtoolbar button[itemId=create]': {
                'click': this.doCreateOnList
            },
            'employeelist gridtoolbar button[itemId=modify]': {
                'click': this.doModifyOnList
            },
            'employeelist gridtoolbar button[itemId=read]': {
                'click': this.doReadOnList
            },
            'employeelist gridtoolbar button[itemId=remove]': {
                'click': this.doRemoveOnList
            },
            'employeeedit': {
                'afterrender': this.doLoadOnEdit
            },
            'employeeedit button[action=save]': {
                'click': this.doSaveOnEdit
            },
            'employeeedit menuitem[action=save_close]': {
                'click': this.doSaveCloseOnEdit
            },
            'employeeedit button[action=create]': {
                'click': this.doCreateOnEdit
            },
            'employeeedit button[action=close]': {
                'click': this.doCloseOnEdit
            }
        });
    },
    
	doClickOnTree: function(view, record, item, index, e, options) {
		var store = this.getEmployeeList().down('grid').store,
			filter = new Array(),
			id = record.get('id');
		filter.push({
			property: 'deptId',
			value: id
		});
		store.proxy.extraParams = {
			filter: Ext.encode(filter)
		};
		store.load({start: 0});
	},
	
	doBeforeCreateOnList: function(edit) {
		var record = this.getEmployeeList().tree.getSelectionModel().getSelection()[0];
		if (record) {
			var deptId = record.get(record.idProperty);
			edit.down('textfield[name=deptId]').setValue(deptId);
		}
	}
});